<style>
.img { background: #fff; width: 75px; padding: 5px 5px 15px 5px; margin: 0 2px; border: 1px solid #666; display: inline-block; }
.img img { cursor: pointer; }
.img td { font-size: 11px !important; }
.img i { cursor: pointer !important; }
</style>
<?php 
$_SESSION[TINYMCE_UPLOAD] = 'upload/plugins/' . basename(dirname(__FILE__)) . '/';
if (!file_exists('../' . $_SESSION[TINYMCE_UPLOAD])) {
  Utils::create('../' . $_SESSION[TINYMCE_UPLOAD]);
}

$id = Utils::get_param('id', 0);
$opts = new stdClass();
$opts->cd = 't.id = :v1';
$opts->cdv = array(':v1' => $id);
$data = $orm->get('pb_ht_newsletter', NULL, NULL, NULL, NULL, $opts)->d;

$pg = Utils::get_param('pg', 1);
$objs = $orm->get('pb_ht_newsletter', 't.id', 'DESC', $pg, SIZE, NULL);
?>
<div class="pure-form pure-form-aligned" style="padding: 10px; width: 790px;">
  <legend>系统</legend>
  
  <div class="pure-control-group">
    <label>状态</label>
    <input id="status" type="text" class="pure-input-1-2" readonly="true" />
  </div>
  
  <legend>用户</legend>
  
  <div class="pure-control-group">
    <label>日常居住地</label>
    <div id="living_country" style="text-align: left; padding-left: 11em; margin-top: -1.5em; min-height: 30px;">&nbsp;</div>
  </div>
  
  <div class="pure-control-group">
    <label>家庭收入</label>
    <div id="family_income" style="text-align: left; padding-left: 11em; margin-top: -1.5em; min-height: 30px;">&nbsp;</div>
  </div>
  
  <div class="pure-control-group">
    <label>准备购买</label>
    <div id="investment_type" style="text-align: left; padding-left: 11em; margin-top: -1.5em; min-height: 30px;">&nbsp;</div>
  </div>
  
  <div class="pure-control-group">
    <label>目标区域</label>
    <div id="interested_area" style="text-align: left; padding-left: 11em; margin-top: -1.5em; min-height: 30px;">&nbsp;</div>
  </div>
  
  <div class="pure-control-group">
    <label>房型</label>
    <div id="property_type" style="text-align: left; padding-left: 11em; margin-top: -1.5em; min-height: 30px;">&nbsp;</div>
  </div>
  
  <div class="pure-control-group">
    <label>卧室数量</label>
    <div id="bedroom_num" style="text-align: left; padding-left: 11em; margin-top: -1.5em; min-height: 30px;">&nbsp;</div>
  </div>
  
  <div class="pure-control-group">
    <label>价格区间</label>
    <div id="price_range" style="text-align: left; padding-left: 11em; margin-top: -1.5em; min-height: 30px;">&nbsp;</div>
  </div>
  
  <legend>邮件</legend>
  
  <div class="pure-control-group">
    <label>标题</label>
    <input id="subject" type="text" class="pure-input-1-2" placeholder="{New Subject" />
  </div>
  
  <div class="pure-control-group">
    <label>内容</label>
    <label><textarea id="tinymce"></textarea></label>
  </div>
  
  <div class="pure-control-group">
    <label>&nbsp;</label>
    <button class="pure-button pure-input-1-2 pure-button-primary" onclick="submit_obj();">Save</button>
  </div>
  
  <div class="pure-control-group expg">
    <label>&nbsp;</label>
    <button id="del_obj" class="pure-button" onclick="delete_obj();">Delete</button>
    <button id="new_obj" class="pure-button" onclick="__data = []; __files = []; reset(); render();">New Email</button>
  </div>
</div>

<script type="text/javascript">
  /*<![CDATA[*/
  var __objs = JSON.parse('<?php echo addslashes(json_encode($objs)); ?>');
  var __data = JSON.parse('<?php echo addslashes(json_encode($data)); ?>');
  var __file = '<?php echo addslashes($app_data->param_file); ?>';
  var __requrl = '<?php echo Utils::get_requrl($app_data); ?>';
  var __redmd = '<?php echo addslashes($app_data->sys->redmd); ?>';
  var __sys = '<?php echo addslashes($app_data->sys->php_name); ?>';
  var __id = '<?php echo $app_data->param_id; ?>';
  var __dir = '<?php echo PLGN_DIR . basename(dirname(__FILE__)); ?>';

  var __living_countries = JSON.parse('<?php echo addslashes(json_encode($LIVING_COUNTRIES)); ?>');
  var __family_incomes = JSON.parse('<?php echo addslashes(json_encode($FAMILY_INCOMES)); ?>');
  var __investment_types = JSON.parse('<?php echo addslashes(json_encode($INVEST_TYPES)); ?>');
  var __interested_areas = JSON.parse('<?php echo addslashes(json_encode($NZ_AREAS)); ?>');
  var __property_types = JSON.parse('<?php echo addslashes(json_encode($HOUSE_TYPES)); ?>');
  var __bdrm_num = JSON.parse('<?php echo addslashes(json_encode($BDRM_NUM)); ?>');
  var __price_ranges = JSON.parse('<?php echo addslashes(json_encode($PRICE_RANGE)); ?>');
  

  
  $(function(){
    reset();
    
    
    
    $('#tinymce').val(__data[0].content);
    tinyMCE.init( {
      // General options
      mode: 'exact',
      elements: 'tinymce',
      theme: 'advanced',
      height: 500,

      plugins: 'pdw,filebrowser,autolink,lists,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist',
      // Theme options
      theme_advanced_buttons1: 'pdw_toggle,|,undo,redo,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,fontselect,fontsizeselect,|,forecolor,backcolor,|,image,media,|,code,preview,|,fullscreen',
      theme_advanced_buttons2: 'search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,link,unlink,anchor,cleanup,help,|,insertdate,inserttime',
      theme_advanced_buttons3: 'tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,advhr,|,print,|,ltr,rtl',
      theme_advanced_buttons4: 'insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak',
      theme_advanced_toolbar_location: 'top',
      theme_advanced_toolbar_align: 'left',
      theme_advanced_statusbar_location: 'bottom',
      theme_advanced_resizing: true,

      file_browser_callback: 'filebrowser',
      pdw_toggle_on: '1',
      pdw_toggle_toolbars: '2,3,4',

      theme_advanced_resizing: true,
      theme_advanced_resize_horizontal: false,
      
      // Example content CSS (should be your site CSS)
      theme_advanced_font_sizes: '10px,11px,12px,13px,14px,15px,16px,17px,18px,19px,20px,21px,22px,23px,24px,25px,26px,27px,28px,29px,30px',
    });

    render();
  });

  function reset() {
    if (__data.length == 0) {
      __data[0] = {
        id: 0,
        search: {
          living_country: [],
          family_income: [],
          investment_type: [],
          interested_area: [],
          property_type: [],
          bedroom_num: [],
          price_range: []
        },
        subject: '',
        content: '',
        sent: '0000-00-00 00:00:00'
      };
    } else {
      __data[0].search = Utils.parse(__data[0].search);
    }
  };

  function render() {
    Utils.leftbar(
      __objs,
      __data[0].id,
      '群发邮件',
      'subject',
      function(page) {
        Url.redirect(__redmd, __sys + '.php?i=' + __id + '&__file=' + __file + '&id=' + __data[0].id + '&pg=' + page);
      }
    );
    render_data();
  };
  
  function render_data() {
    Url.insert_param('id', __data[0].id);

    $('#status').val(((__data[0].sent && __data[0].sent != '0000-00-00 00:00:00') ? '已发送 (' + __data[0].sent + ')' : '未发送'));
    
    $('#subject').val(__data[0].subject);
    if (tinyMCE.get('tinymce')) {
      tinyMCE.get('tinymce').setContent(__data[0].content);
    } else {
      $('#tinymce').val(__data[0].content);
    }

    __render_search('living_country', __living_countries);
    __render_search('family_income', __family_incomes);
    __render_search('investment_type', __investment_types);
    __render_search('interested_area', __interested_areas);
    __render_search('property_type', __property_types);
    __render_search('bedroom_num', __bdrm_num);
    __render_search('price_range', __price_ranges);
    
    if (__data[0].id != 0) {
      $('#del_obj').show();
      $('#new_obj').show();
    } else {
      $('#del_obj').hide();
      $('#new_obj').hide();
    }

    if (__data[0].sent == '0000-00-00 00:00:00' && __data[0].id != 0) {
      $('#del_obj').show();
    } else {
      $('#del_obj').hide();
    }

    if (__data[0].sent && __data[0].sent != '0000-00-00 00:00:00') {
      $('input').prop('disabled', 'disabled');
    } else {
      $('input').removeAttr('disabled');
    }
  };

  function submit_obj() {

    __set_search('living_country');
    __set_search('family_income');
    __set_search('investment_type');
    __set_search('interested_area');
    __set_search('property_type');
    __set_search('bedroom_num');
    __set_search('price_range');
    
    req = Utils.clone(__data[0]);
    req.subject = $('#subject').val();
    req.content = tinyMCE.get('tinymce').getContent();
    req.__pg = __objs.p;
    req.search = JSON.stringify(req.search);
    
    Ajax.request(
      __requrl + 'submit_obj',
      'req=' + Utils.encodeURI(JSON.stringify(req)),
      function(rslt) {
        if (rslt.__objs) {
          __objs = rslt.__objs;
          delete rslt.__objs;
        }
        __data[0] = rslt;
        reset();
        render();
      },
      null
    );
  };

  function __render_search(attr, arr) {
    $('#' + attr).empty();
    for (var idx in arr) {
      var itm = arr[idx];
      var unique = Utils.unique();
      $('#' + attr).append('<input' + (($.inArray(itm, __data[0].search[attr]) != -1) ? ' checked="checked"' : '') + ' type="checkbox" value="' + itm + '" id="' + unique + '" /><label for="' + unique + '" style="margin-right: 0;" title="' + itm + '">' + Utils.get_short(itm, 6) + '</label>');
    }
    $('#' + attr).buttonset();
    $('#' + attr).buttonset().find('label').width(100);
  };
  
  function __set_search(attr) {
    __data[0].search[attr] = [];
    $('#' + attr +  ' :checked').each(function() {
      __data[0].search[attr].push($(this).attr('value'));
    });;
  };
  
  function delete_obj() {
    Ajax.request(
      __requrl.replace(__file, 'index') + 'delete_obj',
      'req=' + Utils.encodeURI(JSON.stringify(__data[0])),
      function(rslt) {
        Url.redirect(__redmd, __sys + '.php?i=' + __id);
      },
      { confirm: 1 }
    );
  };

  function get_obj(id) {
    var req = {
      id: id
    };
    Ajax.request(
      __requrl + 'get_obj',
      'req=' + Utils.encodeURI(JSON.stringify(req)),
      function(rslt) {
        __data = rslt;
        reset();
        render();
      },
      null
    );
    return false;
  };
  /*]]>*/
</script>